#!/bin/sh
PREFIX=apertium-en-ru
LANG1=en
LANG2=ru
FILTERTAG="<prn><enc>"

case $3 in
us|US) MON="ru" MONB=en_US; BIL=ru-en_US ;;
val)   MON="$1" MONB=val-ru; BIL=val-en-ru ;;
*)     MON=$1; BIL=$1-$2;;
esac

# FASE 1
echo "=======================";
echo "Comprovació superficial";
echo "=======================";
echo -n "Calculant expansió inicial...";
lt-expand .deps/$MON.dix | awk 'BEGIN{FS=":"}{if($2!="<") printf("^.<sent>$ ^%s$\n",$NF)}' |apertium-pretransfer | grep -v "REGEXP" > comp-$BIL.expand
echo " fet.";
echo -n "Calculant expansió filtrada...";
grep -v "$FILTERTAG" comp-$BIL.expand >comp-$BIL.filtered
echo " fet.";
echo -n "Executant el traductor..."
apertium-transfer $PREFIX.$1-$2.t1x $1-$2.t1x.bin $BIL.autobil.bin < comp-$BIL.filtered | \
apertium-interchunk $PREFIX.$1-$2.t2x $1-$2.t2x.bin | \
apertium-postchunk $PREFIX.$1-$2.t3x $1-$2.t3x.bin | \
lt-proc -d $BIL.autogen.bin > comp-$BIL.trans
echo " fet.";
echo -n "Detectant errades i guardant-les en errors-$BIL.superficial..."
paste comp-$BIL.filtered comp-$BIL.trans > comprovacio-$BIL
egrep "(@|/|. #)" comprovacio-$BIL | grep -v '\\\/' > errors-$BIL.superficial
echo " fet."
echo "==========================" >> errors-$BIL.superficial
echo "ALTRES ERRADES DE BILINGÜE" >> errors-$BIL.superficial
echo "==========================" >> errors-$BIL.superficial
echo -n "Convertint diccionari bilingüe... "
xsltproc translate-to-default-equivalent.xsl .deps/$BIL.bil.dix > $$.dix
echo " fet."
echo -n "Expandint diccionari bilingüe... "
if [ $1-$2 = $LANG1-$LANG2 ]
then lt-expand $$.dix | awk 'BEGIN{FS=":"}{if($2!="<") printf("^.<sent>$ ^%s$\n",$1)}' | grep -v "REGEXP" > $$.expand
else lt-expand $$.dix | awk 'BEGIN{FS=":"}{if($2!="<") printf("^.<sent>$ ^%s$\n",$NF)}' | grep -v "REGEXP" > $$.expand
fi
echo " fet."
echo -n "Compilant diccionari convertit... "
if [ $1-$2 = $LANG1-$LANG2 ]
then lt-comp lr $$.dix $$.bin >/dev/null
else lt-comp rl $$.dix $$.bin >/dev/null
fi
echo " fet."
rm $$.dix
echo -n "Detectant errades i guardant-les en errors-$BIL.superficial..."
awk 'BEGIN{FS=":";}{if($2 == ">") print "^" $1 "$"; else if($2=="<"); else print "^" $1 "$";}' <$$.expand|lt-proc -d $$.bin |grep "/" >>errors-$BIL.superficial
echo " fet."a
rm $$.bin
#rm $$.expand
echo "";
echo "Comprovació superficial finalitzada. Mireu en 'errors-$BIL.superficial'"

echo ""
echo "===================="
echo "Comprovació completa"
echo "===================="

# FASE 2
echo "Continuant comprovació completa, ^C per a cancelar"
echo -n "Calculant expansió filtrada..."
grep "$FILTERTAG" comp-$BIL.expand >comp-$BIL.filtered
echo " fet."
echo -n "Executant el traductor..."
apertium-transfer $PREFIX.$1-$2.t1x $1-$2.t1x.bin $BIL.autobil.bin < comp-$BIL.filtered | \
apertium-interchunk $PREFIX.$1-$2.t2x $1-$2.t2x.bin | \
apertium-postchunk $PREFIX.$1-$2.t3x $1-$2.t3x.bin | \
lt-proc -d $1-$2.autogen.bin > comp-$BIL.trans
echo " fet."
echo -n "Detectant errades i guardant-les en errors-$BIL"
paste comp-$BIL.filtered comp-$BIL.trans > comprovacio-$BIL
egrep "(@|/|. #)" comprovacio-$BIL > errors-$BIL
echo " fet."
